
package com.wyzz.hr.bo.impl;

import java.io.*;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.*;
import org.xml.sax.SAXException;

import com.wyzz.hr.base.FileOperate;
import com.wyzz.hr.bo.LogBo;
import com.wyzz.hr.util.DatabaseSysConfigManager;
import com.wyzz.hr.util.Pager;

public class LogBoImpl implements LogBo {

	public LogBoImpl() {
	}

	public synchronized ArrayList getLogRecorders(String filePath, Pager pager) {
		ArrayList list;
		File file = new File(filePath);
		if (!file.exists()) {
			File fileD = new File(filePath.substring(0, filePath
					.lastIndexOf("/")));
			fileD.mkdirs();
			clearLogRecorders(filePath);
		}
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder;
		Document document = null;
		try {
			builder = factory.newDocumentBuilder();
			document = builder.parse(new File(filePath));
		} catch (ParserConfigurationException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Node node = document.getFirstChild();
		node = node.getFirstChild();
		int totalLength = Integer.parseInt(node.getTextContent());
		splitPagers(totalLength, pager);
		list = new ArrayList();
		int pageCount = 0;
		int begain = totalLength - pager.getCurrentPage() * pager.getPageSize();
		begain = begain <= 0 ? 0 : begain;
		int end = begain + pager.getPageSize();
		end = end <= totalLength ? end : totalLength;
		do {
			if ((node = node.getNextSibling()) == null)
				break;
			if (pageCount < begain) {
				pageCount++;
				continue;
			}
			if (pageCount >= end)
				break;
			if (node.getNodeName().equals("C")) {
				pageCount++;
				list.add(node.getTextContent());
			}
		} while (true);
		int listSize = list.size();
		if (listSize > 0) {
			ArrayList list2 = new ArrayList();
			for (int i = 0; i < listSize; i++)
				list2.add(list.get(listSize - i - 1));

			return list2;
		}
		try {
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ArrayList();
	}

	public synchronized boolean clearLogRecorders(String fileName) {
		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document document = builder.newDocument();
			Element root = document.createElement("R");
			document.appendChild(root);
			Element title = document.createElement("T");
			title.appendChild(document.createTextNode("0"));
			root.appendChild(title);
			TransformerFactory tf = TransformerFactory.newInstance();
			Transformer transformer = tf.newTransformer();
			DOMSource source = new DOMSource(document);
			transformer.setOutputProperty("encoding", "Shift_JIS");
			PrintWriter pw = new PrintWriter(new FileOutputStream(fileName));
			StreamResult result = new StreamResult(pw);
			transformer.transform(source, result);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public synchronized boolean updateXML(String fileName, String content) {
		try {
			System.out.println(fileName);
			File file = new File(fileName);
			if (!file.exists()) {
				File fileD = new File(fileName.substring(0, fileName
						.lastIndexOf("/")));
				fileD.mkdirs();
				clearLogRecorders(fileName);
			}
			DocumentBuilderFactory factory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document document = builder.parse(new File(fileName));
			Node node = document.getFirstChild();
			Node titleNode = node.getFirstChild();
			System.out.println(titleNode.getTextContent());
			int nodeLength = Integer.valueOf(titleNode.getTextContent())
					.intValue();
			titleNode.setTextContent(String.valueOf(nodeLength + 1));
			Element contentN = document.createElement("C");
			contentN.appendChild(document.createTextNode(content));
			node.appendChild(contentN);
			DOMSource doms = new DOMSource(document);
			File f = new File(fileName);
			StreamResult sr = new StreamResult(f);
			TransformerFactory tf = TransformerFactory.newInstance();
			Transformer t = tf.newTransformer();
			t.transform(doms, sr);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public void splitPagers(int size, Pager pager) {
		int pageSize = Integer.valueOf(
				DatabaseSysConfigManager.getInstance().getProperty(
						"sys.split.pages")).intValue();
		pager.init(size, pageSize);
		if (pager.getOperate() != null
				&& "previous".equalsIgnoreCase(pager.getOperate()))
			pager.previous();
		if (pager.getOperate() != null
				&& "next".equalsIgnoreCase(pager.getOperate()))
			pager.next();
		if (pager.getOperate() != null
				&& "first".equalsIgnoreCase(pager.getOperate()))
			pager.first();
		if (pager.getOperate() != null
				&& "last".equalsIgnoreCase(pager.getOperate()))
			pager.last();
	}

	public static void main(String args[]) {
		LogBoImpl lb = new LogBoImpl();
		lb.getLogRecorders((new StringBuilder()).append(
				FileOperate.getFileHomePath()).append("login_log/login.xml")
				.toString(), new Pager());
	}
}
